package com.richfit.barcodesystem_cczt.fragments.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;

import com.bigkoo.alertview.AlertView;
import com.richfit.barcodesystem_cczt.databinding.LayoutUnpackingItemBinding;
import com.richfit.barcodesystem_cczt.fragments.model.ZBoxEntity;

import java.util.List;

public class Unpack_Adapter extends BaseAdapter {
    private LayoutUnpackingItemBinding binding;
    private Context mContext;
    private List<ZBoxEntity.Detail> list;
    private int mEditTextIndex = -1;

    public Unpack_Adapter(Context context, List<ZBoxEntity.Detail> list) {
        this.mContext = context;
        this.list = list;
    }

    private ItemEditTextListener itemEditTextListener;

    public void setOnItemEditTextListener(ItemEditTextListener itemClickListener) {
        this.itemEditTextListener = itemClickListener;

    }

    public interface ItemEditTextListener {
        void onItemEditText(EditText editText, int position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ZBoxEntity.Detail getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null) {
            binding = LayoutUnpackingItemBinding.inflate(LayoutInflater.from(mContext));
            view = binding.getRoot();
            view.setTag(binding);
        } else {
            binding = (LayoutUnpackingItemBinding) view.getTag();
        }
        ZBoxEntity.Detail detail = getItem(position);
        //序号
        binding.TxtBoxNo.setText(detail.getLineNum());
        //物料编码
        binding.TxtaterialId.setText(detail.getMaterialNum());
        binding.TxtaterialId.setOnClickListener(v -> showMsg(detail.getMaterialNum()));
        //计量单位
        binding.TxtUnit.setText(detail.getUnit());
        //中文描述
        binding.TxtPackingSpec.setText(detail.getMaterialDesc());
        binding.TxtPackingSpec.setOnClickListener(v -> showMsg(detail.getMaterialDesc()));

        //外文描述
        binding.TxtMaterialEdesc.setText(detail.getMaterialEdesc());
        binding.TxtMaterialEdesc.setOnClickListener(v -> showMsg(detail.getMaterialEdesc()));

        //箱单数量
        binding.TxtInQuantity.setText(detail.getInQuantity() + "");
        //移动数量,监控输入的数量
        itemEditTextListener.onItemEditText(binding.EdtMoveQuantity, position);
        return view;
    }

    AlertView alertView;

    void showMsg(String msg) {
        alertView = new AlertView("详细信息", msg, null,
                new String[]{"确定"}, null, mContext,
                AlertView.Style.Alert, (o, position1) -> {
            alertView.dismiss();
        });
        alertView.show();
    }
}
